package org.arkeologgr.appening.service;

import java.util.ArrayList;
import java.util.List;

import org.arkeologgr.appening.entity.Item;
import org.arkeologgr.appening.entity.ItemDetails;
import org.arkeologgr.appening.entity.Item.ItemSource;
import org.arkeologgr.appening.util.PresItem;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;


public class KSamAdapter {

	static final String BASE_URL = "http://kulturarvsdata.se/ksamsok/api?method=search";
	static final String API_KEY = "&x-api=test";
	static final String BOUNDING_BOX = "&query=boundingBox=/WGS84 \"";
		
	public List<ItemDetails> getArea(double west, double south, double east, double north) {
		StringBuilder url = new StringBuilder(BASE_URL);
		url.append(BOUNDING_BOX);
		url.append(west + " ");
		url.append(south + " ");
		url.append(east + " ");
		url.append(north + "\"");
		url.append(API_KEY);

		SAXReader reader = new SAXReader();
		
		Document document;
        try {
			document = reader.read(url.toString());
			// System.out.println(document.asXML());
		} catch (DocumentException e) {
			throw new RuntimeException(e);
		}

		List<ItemDetails> itemDetails = new ArrayList<ItemDetails>();
		List<Node> nodes = PresItem.getItems(document);
		for(Node node : nodes) {
			PresItem presItem = new PresItem(node);
			Item item = new Item();
		
			item.setEntityUri(presItem.getPresEntityUri());
			item.setItemSource(ItemSource.KSam);
			String[] coordinate = presItem.getPresCoordinates().split(",");
			item.setLongitude(Double.parseDouble(coordinate[0]));
			item.setLatitude(Double.parseDouble(coordinate[1]));
			item.setThumbUrl(presItem.getPresThumbnail());
			item.setTitle(presItem.getPresType());
			
			ItemDetails itemDetail = new ItemDetails();
			itemDetail.setItem(item);
			itemDetail.setDescription(presItem.getPresDescription());
			itemDetail.setImageUrl(presItem.getPresLowRes());
			
			itemDetails.add(itemDetail);
			
		}

		return itemDetails;
	}

	public ItemDetails getItemDetails(String entityUri) {

		SAXReader reader = new SAXReader();
		
		Document document;
        try {
			document = reader.read(entityUri);
		} catch (DocumentException e) {
			System.out.println("uri = " + entityUri);
			throw new RuntimeException(e);
		}

		List<Node> nodes = PresItem.getItems(document);
		if(nodes.size() == 0) {
			return null;
		}
		
		PresItem presItem = new PresItem(nodes.get(0));
		Item item = new Item();
		
		item.setEntityUri(presItem.getPresEntityUri());
		item.setItemSource(ItemSource.KSam);
		String[] coordinate = presItem.getPresCoordinates().split(",");
		item.setLongitude(Double.parseDouble(coordinate[0]));
		item.setLatitude(Double.parseDouble(coordinate[1]));
		item.setThumbUrl(presItem.getPresThumbnail());
		item.setTitle(presItem.getPresType());

		ItemDetails itemDetails = new ItemDetails();
		itemDetails.setItem(item);
		itemDetails.setDescription(presItem.getPresDescription());
		itemDetails.setImageUrl(presItem.getPresLowRes());
		return itemDetails;
	}

}
